const express = require('express');
const path = require('path');
const app = express();

// 中间件配置
app.use(express.json());
app.use(express.static(__dirname));

// 登录API端点
app.post('/api/login', (req, res) => {
    console.log('收到登录请求:', req.body);
    
    // 模拟登录验证
    const { username, password } = req.body;
    
    // 简单的验证逻辑
    if (username && password) {
        // 模拟成功响应
        res.json({
            success: true,
            message: '登录成功',
            token: 'mock_jwt_token_' + Date.now(),
            user: {
                id: 1,
                username: username,
                email: username + '@example.com'
            }
        });
    } else {
        // 模拟失败响应
        res.status(401).json({
            success: false,
            message: '用户名或密码错误'
        });
    }
});

// 模拟登录成功后的页面
app.get('/dashboard.html', (req, res) => {
    res.send(`
        <!DOCTYPE html>
        <html lang="zh-CN">
        <head>
            <meta charset="UTF-8">
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
            <title>登录成功 - 仪表盘</title>
            <link href="https://cdn.tailwindcss.com" rel="stylesheet">
        </head>
        <body class="flex items-center justify-center min-h-screen bg-gradient-to-r from-green-400 to-blue-500">
            <div class="text-center text-white">
                <h1 class="text-4xl font-bold mb-4">🎉 登录成功！</h1>
                <p class="text-xl mb-8">欢迎来到仪表盘页面</p>
                <a href="/" class="bg-white text-blue-500 font-bold py-3 px-6 rounded-full hover:bg-gray-100 transition duration-300">
                    返回登录页
                </a>
            </div>
        </body>
        </html>
    `);
});

// 启动服务器
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
    console.log(`服务器运行在 http://localhost:${PORT}`);
    console.log('访问 http://localhost:' + PORT + ' 查看登录按钮示例');
});
